package com.police.view;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.police.R;
import com.police.activity.MainActivity;
import com.police.model.SearchRecordBean;
/**
 * 点击搜索记录时弹出框
 * @author yinxing
 *
 */
public class SearchRecordPop extends LinearLayout{
	
	MainActivity main;
	ListView recordList;
	public LinearLayout layout;
	LayoutInflater inflater;
	LayoutParams layoutParams;
	SearchRecordAdapter adapter;
	ArrayList<SearchRecordBean>searchRecord=new ArrayList<SearchRecordBean>();
	public SearchRecordPop(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		main=(MainActivity) context;
		initView();
	}

	private void initView() {
		inflater = LayoutInflater.from(main);
		layout = (LinearLayout) inflater.inflate(R.layout.search_record_view, null);
		layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);
		recordList = (ListView) layout.findViewById(R.id.record_list);
		this.addView(layout, layoutParams);
		adapter = new SearchRecordAdapter();
		if (adapter != null) {
			recordList.setAdapter(adapter);
		}
	for (int i = 0; i < 10; i++) {
	
		SearchRecordBean bean=new SearchRecordBean();
		bean.Count=i;
		bean.Name="郑重";
		searchRecord.add(bean);
	}
	
	recordList.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			//popupWindow();
		}
	});
	}

	public void popupWindow(){

		SearchRecordDetailsPop  popup = new SearchRecordDetailsPop(main);
		PopupWindow popupWin = createWin(popup);
		popupWin.showAtLocation(layout, Gravity.BOTTOM, -100, 50);
		popupWin.update();
		}
	
	private PopupWindow createWin(final SearchRecordDetailsPop subview) {



		final PopupWindow win = new PopupWindow(subview,
		200, 300);
		win.setFocusable(true);
		win.setBackgroundDrawable(new BitmapDrawable());
		win.setOutsideTouchable(false);
		return win;
		
		}

	
	
	
	
	
	private class SearchRecordAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 10;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			SearchRecordBean bean = null;
			bean = searchRecord.get(position);
			if (bean == null) {
				return null;
			}
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.search_record_item, null);
				holder = new ViewHolder();
				holder.name = (TextView) convertView
						.findViewById(R.id.name);
				holder.count = (TextView) convertView
						.findViewById(R.id.count);
				convertView.setTag(holder);
			}
			holder = (ViewHolder) convertView.getTag();
			holder.name.setText("郑重");
			holder.count.setText("5");
			return convertView;
		}

	}

	static class ViewHolder {

		public TextView name;
		public TextView count;
	}
}

